package com.fbt.andon.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fbt.andon.domain.LittleGroup;
import com.fbt.andon.domain.StopLine;
import com.fbt.andon.service.impl.GroupServiceImpl;

import com.linkwechat.common.annotation.Log;
import com.linkwechat.common.core.controller.BaseController;
import com.linkwechat.common.core.domain.AjaxResult;
import com.linkwechat.common.enums.BusinessType;
import com.linkwechat.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.acl.Group;
import java.util.Arrays;
import java.util.List;

/**
 * 小组Controller
 *
 * @author fengtao
 * @date 2021-08-27
 */
@RestController
@RequestMapping("/group")
@Api(description = "小组api")
public class GroupController extends BaseController {
    @Autowired
    private GroupServiceImpl groupService;


    @GetMapping("/query")
    @ApiOperation("查询所有问小组列表")
    //@PreAuthorize(hasPermi = "mes:andon:query")
    public AjaxResult findIssueList() {
        startPage();
        return AjaxResult.success("查询成功", groupService.list(null));
    }

    //@PreAuthorize(hasPermi = "mes:andon:query")
    @ApiOperation("根据id查询人员")
    @GetMapping(value = "/{id}")
    public AjaxResult getByid(@PathVariable("id") Integer id) {
        return AjaxResult.success("获取成功", groupService.getById(id));
    }

    //@PreAuthorize(hasPermi = "mes:andon:query")
    @PostMapping("/findByQueryParams/{groupName}")
    @ApiOperation("根据小组名模糊查询停线列表")
    public AjaxResult queryList(@PathVariable("groupName") String groupName) {
        startPage();
        QueryWrapper<LittleGroup> wrapper = new QueryWrapper<>();
        wrapper.like("group_name", groupName);
        return AjaxResult.success("查询成功", groupService.list(wrapper));
    }

    /**
     * 获取小组详细信息
     */
    //@PreAuthorize(hasPermi = "mes:andon:query")
    @GetMapping("/getGroup/{id}")
    @ApiOperation("根据id获取小组详情")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success("获取成功", groupService.getById(id));
    }

    /**
     * 新增小组
     *
     * @return
     */
    //@PreAuthorize(hasPermi = "mes:andon:add")
    @ApiOperation("新增小组")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LittleGroup littleGroup) {
//        System.out.println(littleGroup.setId(300));
        return AjaxResult.success("新增成功", groupService.save(littleGroup));
    }


    /**
     * 修改小组
     *
     * @return
     */
    //@PreAuthorize(hasPermi = "mes:andon:edit")
    @ApiOperation("修改小组")
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody LittleGroup littleGroup) {
        System.out.println("====");
        return AjaxResult.success("修改成功", groupService.updateById(littleGroup));
    }

    /**
     * 删除小组
     */
    //@PreAuthorize(hasPermi = "mes:andon:remove")
    @ApiOperation("批量删除小组")
    @PostMapping("/batchRemove/{ids}")
    public AjaxResult batchRemove(@PathVariable Long[] ids) {
        System.out.println(ids);
        return AjaxResult.success("删除成功", groupService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 导出产线列表
     */
    //@PreAuthorize(hasPermi = "mes:andon:export")
    @Log(title = "产线", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LittleGroup littleGroup) throws IOException {
        List<LittleGroup> list = groupService.list(null);
        ExcelUtil<LittleGroup> util = new ExcelUtil<LittleGroup>(LittleGroup.class);
        //util.exportExcel(response, list, "littleGroup");
    }
}
